import { v4 as uuidv4 } from 'uuid'
export type NodeType = 'base' | 'common'| 'decision' | 'start' | 'option'

export default class BaseNode {

  desc:string=''

  title:string = ''

  uuid: string = ''

  type: string = ''

  next: string = '' // 下一跳

  prev: string = ''  // 上一跳

  action: string = ''

  constructor (uuid?:string) {
    this.uuid = uuid || uuidv4()
  }

  toJSON:Function=() => {
    const obj2Json = (obj:Record<string, any>)=>{
      const json:Record<string, any> = {}
      const keys:string[] = Object.keys(obj)
      for(let key of keys){
        if(obj[key] instanceof Function){continue}
        if(Array.isArray(obj[key])){
          json[key] = obj[key]
          continue
        }
        if(obj[key] instanceof Object){
          json[key]=obj2Json(obj[key])
        }
        if(typeof obj[key] ==='string' || typeof obj[key]==='number'){
          json[key] = obj[key]
        }
      }
      return json
    }
    return obj2Json(this)
  }

  toObj:Function=(file:Record<string, any>) => {
    const keys = Object.keys(file)
    for(let key of keys){
      (this as any)[key] = file[key]
    }
    return this
  }
}
